﻿using BinarySplitter;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Text;

namespace BinarySplitter
{

    [TestClass()]
    public class BinarySplitterTest
    {

        [TestMethod()]
        public void CreateBinaryFileTest()
        {
            string filename = "pesho.bin";
            byte[] informationToWrite = Encoding.Unicode.GetBytes("pesho e mnogo gotin pich i ima moskvich");
            BinarySplitter.CreateBinaryFile(filename, informationToWrite);
            byte[] readBytes = File.ReadAllBytes("pesho.bin");

            Assert.AreEqual(readBytes.Length, informationToWrite.Length);

            for (int i = 0; i < readBytes.Length; i++)
            {
                Assert.AreEqual(informationToWrite[i], readBytes[i]);
            }
        }

        [TestMethod()]
        public void CreateBinaryFileWhenFileExistsTest()
        {
            string filename = "pesho.bin";
            byte[] informationToWrite = Encoding.Unicode.GetBytes("pesho e mnogo gotin pich i ima moskvich");
            BinarySplitter.CreateBinaryFile(filename, informationToWrite);
            BinarySplitter.CreateBinaryFile(filename, informationToWrite);

        }

        [TestMethod()]
        public void SplitBinaryFirstFileSizeTest()
        {
            string filename = "pesho.bin";
            BinarySplitter.SplitBinaryFile(filename);

            FileInfo originalFile = new FileInfo("pesho.bin");
            long originalFileLength = originalFile.Length;

            FileInfo firstFile = new FileInfo("firstPart_pesho.bin");
            Assert.AreEqual(firstFile.Length, originalFileLength / 2);
        }

        [TestMethod()]
        public void SplitBinarySecondFileSizeTest()
        {
            string filename = "pesho.bin";
            BinarySplitter.SplitBinaryFile(filename);

            FileInfo originalFile = new FileInfo("pesho.bin");
            long originalFileLength = originalFile.Length;

            FileInfo firstFile = new FileInfo("secondPart_pesho.bin");
            Assert.AreEqual(firstFile.Length, originalFileLength / 2);
        }

        [TestMethod()]
        public void UniteTwoFilesTest_CheckingForEqualContentAfterUniting()
        {
            string firstFilename = "firstPart_pesho.bin"; // TODO: Initialize to an appropriate value
            string secondFilename = "secondPart_pesho.bin"; // TODO: Initialize to an appropriate value
            string resultFilename = "peshoUnited.bin"; // TODO: Initialize to an appropriate value
            BinarySplitter.UniteTwoFiles(firstFilename, secondFilename, resultFilename);

            byte[] firstFileContent = File.ReadAllBytes(firstFilename);
            byte[] secondFileContent = File.ReadAllBytes(secondFilename);
            byte[] resultFile = File.ReadAllBytes(resultFilename);

            int partCounter = 0;
            bool isFirstPartDone = false;
            for (int i = 0; i < resultFile.Length; i++)
            {
                if (isFirstPartDone == false)
                {
                    Assert.AreEqual(firstFileContent[partCounter], resultFile[i]);
                }
                partCounter++;
                if (partCounter == firstFileContent.Length)
                {
                    partCounter = 0;
                    isFirstPartDone = true;
                    i++;
                }
                if (isFirstPartDone)
                {
                    Assert.AreEqual(secondFileContent[partCounter], resultFile[i]);
                }
            }

        }

      
    }
}
